<?php 
namespace app\admin\controller;

use think\Controller;
use app\model\Admin;
use think\Session;
use think\Cookie;
class Index extends Controller{
	// 管理员登录
	public function Index(){
		if(Cookie::has("admin_id",'backend')){
			dump(Cookie::has("admin_id","backend"));
			session::set("admin_name",Cookie::get("admin_name",'backend'));
			session::set("admin_password",Cookie::get("admin_password",'backend'));
			session::set("admin_id",Cookie::get("admin_id",'backend'));
			return $this->redirect('userdo/userlist');
		}
		return $this->fetch();
	}

	// 管理员登录
	public function Login(){

		$validate = \think\Loader::validate('User');
		if(!$validate->check(input('post.'))){
			$this->error($validate->getError());
		}
		$data['admin_name'] = input('admin_name');
		$data['admin_password'] = md5(input('admin_password'));
		$admin = Admin::get($data);
		if(null == $admin){
			return getResponse(1,"用户名或密码错误!");
		}else{
			if(isset($_POST["rm"])){
				Cookie::prefix('backend');
				Cookie::set("admin_id",$admin['admin_id'],['prefix'=>'backend','expire'=>3600*24*30]);
				Cookie::set("admin_password",$admin['admin_password'],['prefix'=>'backend','expire'=>3600*24*30]);
				Cookie::set("admin_name",$admin['admin_name'],['prefix'=>'backend','expire'=>3600*24*30]);
			}
			session::set("admin_name",$admin['admin_name']);
			session::set("admin_password",$admin['admin_password']);
			session::set("admin_id",$admin['admin_id']);
			return $this->redirect('userdo/userlist');
		}
	}

	// 管理员登出
	public function Logout(){
		Cookie::clear("backend");
		session::clear();
		return $this->redirect("index");
	}

	public function resetPassword(){
		$old = session::get("admin_password");
		if($old == md5(input('old'))){
			$admin = Admin::get(input("admin_id"));
			$admin->admin_password = md5(input("new"));
			$admin->save();
			return getResponse(0,"修改成功");
		}
		return getResponse(1,"原密码不正确");
	}
	
}


 ?>